package com.seisys.util;

import org.codehaus.jackson.map.ObjectMapper;

public class JacksonJsonUtil {

	 private static ObjectMapper mapper;

	    
	    public static synchronized ObjectMapper getMapperInstance(boolean createNew) {
	        if (createNew) {
	            return new ObjectMapper();
	        } else if (mapper == null) {
	            mapper = new ObjectMapper();
	        }
	        return mapper;
	    }

	    
	    public static String beanToJson(Object obj) throws Exception {
	        try {
	            ObjectMapper objectMapper = getMapperInstance(false);
	            String json = objectMapper.writeValueAsString(obj);
	            return json;
	        } catch (Exception e) {
	            throw new Exception(e.getMessage());
	        }
	    }

	    
	    public static String beanToJson(Object obj, Boolean createNew) throws Exception {
	        try {
	            ObjectMapper objectMapper = getMapperInstance(createNew);
	            String json = objectMapper.writeValueAsString(obj);
	            return json;
	        } catch (Exception e) {
	            throw new Exception(e.getMessage());
	        }
	    }

	    
	    public static Object jsonToBean(String json, Class<?> cls) throws Exception {
	        try {
	            ObjectMapper objectMapper = getMapperInstance(false);
	            Object vo = objectMapper.readValue(json, cls);
	            return vo;
	        } catch (Exception e) {
	            throw new Exception(e.getMessage());
	        }
	    }

	    
	    public static Object jsonToBean(String json, Class<?> cls, Boolean createNew) throws Exception {
	        try {
	            ObjectMapper objectMapper = getMapperInstance(createNew);
	            Object vo = objectMapper.readValue(json, cls);
	            return vo;
	        } catch (Exception e) {
	            throw new Exception(e.getMessage());
	        }
	    }
	
}
